package Test;

import java.util.LinkedList;
import java.util.List;

public class 二叉树的右视图 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res=new LinkedList<>();
        LinkedList<TreeNode> q=new LinkedList<>();
        if(root==null) return res;
        q.add(root);
        while (!q.isEmpty()){
            int sz=q.size();
            TreeNode last=q.peek();
            for(int i=0;i<sz;i++){
                TreeNode node=q.pop();
                if(node.right!=null){
                    q.offer(node.right);
                }
                if(node.left!= null){
                    q.offer(node.left);
                }
            }
            res.add(last.val);
        }
        return res;
    }
}
